package net.sipvip.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Grid;

public class JsonRequests {
	
	public static void addvideo(String urlstr){
		
		JSONRequest.get(urlstr,

				new JSONRequestHandler() {

					public void onRequestComplete(JavaScriptObject json) {
						
					}
					});
	}
	

	public static void videojsons(FlowPanel flowpanel, String urlstr) {
		final FlowPanel nflpanel = flowpanel;
		final Grid videos = new Grid(5, 5);

		JSONRequest.get(urlstr,

		new JSONRequestHandler() {

			public void onRequestComplete(JavaScriptObject json) {
				JSONArray j = new JSONArray(json);

				for (int i = 0; i < j.size(); ++i) {
					JSONString source_file_name = j.get(i).isObject().get(
							"video").isObject().get("source_file_name")
							.isString();
					JSONNumber id = j.get(i).isObject().get("video").isObject()
							.get("id").isNumber();
					JSONString description = j.get(i).isObject().get("video")
							.isObject().get("description").isString();

					int a = (int) i / 5;
					int b = i;

					if (i > 4) {
						b = i - 5;
					}
					if (i > 9) {
						b = i - 10;
					}
					if (i > 14) {
						b = i - 15;
					}
					if (i > 19) {
						b = i - 20;
					}

					String videostr = "http://89.18.248.118:7900/sources/"
							+ id.toString() + "/original/"
							+ source_file_name.stringValue();
					int mid = source_file_name.stringValue().lastIndexOf(".");
					String imgfile = source_file_name.stringValue().substring(
							0, mid)
							+ ".jpg";
					String videoimage = "http://89.18.248.118:7900/sources/"
							+ id.toString() + "/thumb/" + imgfile;

					JWplayer jwplayer = new JWplayer(videostr, videoimage, description.stringValue(),true);
					DecoratorPanel decoratorpanel = new DecoratorPanel();
					Grid smallgrid = new Grid(2, 1);

					jwplayer.setUrl(videoimage);

					smallgrid.setWidget(0, 0, jwplayer);

					String descriptionstr = "";
					if (description.stringValue().length() > 15) {
						descriptionstr = description.stringValue().substring(0,
								15)
								+ "...";
					} else {
						descriptionstr = description.stringValue();
					}

					smallgrid.setText(1, 0, descriptionstr);
					decoratorpanel.add(smallgrid);
					videos.setWidget(a, b, decoratorpanel);

				}

				nflpanel.add(videos);

			}

		});
	}

	public static void videojsonsYT(FlowPanel flowpanel, String urlstr) {
		// TODO Auto-generated method stub
		final FlowPanel nflpanel = flowpanel;
		final Grid videos = new Grid(5, 5);

		JSONRequest.get(urlstr, new JSONRequestHandler() {

			public void onRequestComplete(JavaScriptObject json) {

				JSONObject j = new JSONObject(json);
				JSONArray jentry = j.get("feed").isObject().get("entry")
						.isArray();
				GWT.log(jentry.toString(), null);
				for (int i = 0; i < jentry.size(); ++i) {

					JSONString description = jentry.get(i).isObject().get(
							"title").isObject().get("$t").isString();

					JSONArray jimg = jentry.get(i).isObject()
							.get("media$group").isObject().get(
									"media$thumbnail").isArray();
					JSONString jimgdefault = jimg.get(0).isObject().get("url")
							.isString();

					JSONObject jvideo = jentry.get(i).isObject().get(
							"media$group").isObject().get("yt$videoid")
							.isObject();
					JSONString jvideourl = jvideo.get("$t").isString();

					int a = (int) i / 5;

					int b = i;

					if (i > 4) {
						b = i - 5;

					}
					if (i > 9) {
						b = i - 10;

					}

					if (i > 14) {
						b = i - 15;

					}

					if (i > 19) {
						b = i - 20;

					}
					
					DecoratorPanel decoratorpanel = new DecoratorPanel();
					Grid smallgrid = new Grid(2, 1);

					YTplayer ytplayer = new YTplayer(jvideourl.stringValue());
					ytplayer.setUrl(jimgdefault.stringValue());
					smallgrid.setWidget(0, 0, ytplayer);

					String descriptionstr = "";
					if (description.stringValue().length() > 15) {
						descriptionstr = description.stringValue().substring(0,
								15)
								+ "...";
					} else {
						descriptionstr = description.stringValue();
					}

					smallgrid.setText(1, 0, descriptionstr);
					decoratorpanel.add(smallgrid);
					videos.setWidget(a, b, decoratorpanel);

				}

				nflpanel.add(videos);


			}

		});

	}
}
